package com.weiyuwqj.server.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.server.service.sys.module.ConfigTicketModule;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.*;
import com.weiyuwqj.api.service.SysConfigTicketRuleService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.model.vo.SysConfigTicketRuleVo;
import com.weiyuwqj.api.model.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huzj on 2016/8/24.
 */
@Service("ticketRuleService")
public class SysConfigTicketRuleServiceImpl extends BaseServiceImpl<SysConfigTicketRule> implements SysConfigTicketRuleService {

    @Autowired
    ConfigTicketModule ticketModule;

    @Autowired
    SysConfigTicketRuleMapper ticketRuleMapper;

    @Autowired
    BasUserMapper basUserMapper;

    @Autowired
    SysConfigTicketUserMapper ticketUserMapper;

    @Autowired
    BasCouponMapper couponMapper;

    @Autowired
    BasTicketMapper ticketMapper;


    @Override
    public Map addTicket(TicketParamEntity paramEntity) {
        return ticketModule.addTicket(paramEntity);
    }

    @Override
    public PageList getCouponList(Paginator p, Map<String, Object> param) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<SysConfigTicketRuleVo> list = ticketRuleMapper.getCouponList(param);
        for (SysConfigTicketRuleVo configTicketRuleVo :list){
            Map receiveParam = new HashMap();
            receiveParam.put("ruleId",configTicketRuleVo.getId());
            Map usedParam = new HashMap();
            usedParam.put("ruleId",configTicketRuleVo.getId());
            usedParam.put("status",1);
            Map overdueParam = new HashMap();
            overdueParam.put("ruleId",configTicketRuleVo.getId());
            overdueParam.put("status",3);
            if(configTicketRuleVo.getTicketType() ==1){
                configTicketRuleVo.setReceiveCount(couponMapper.statCouponCount(receiveParam));
                configTicketRuleVo.setUsedCount(couponMapper.statCouponCount(usedParam));
                configTicketRuleVo.setOverdueCount(couponMapper.statCouponCount(overdueParam));
            }
            else if(configTicketRuleVo.getTicketType() ==2){
                configTicketRuleVo.setReceiveCount(ticketMapper.statTicketCount(receiveParam));
                configTicketRuleVo.setUsedCount(ticketMapper.statTicketCount(usedParam));
                configTicketRuleVo.setOverdueCount(ticketMapper.statTicketCount(overdueParam));
            }
        }
        return new PageList(list);
    }

    @Override
    public PageList getTicketList(Paginator p, Map<String, Object> param) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<SysConfigTicketRuleVo> list = ticketRuleMapper.getTicketList(param);
        for (SysConfigTicketRuleVo configTicketRuleVo :list){
            Map receiveParam = new HashMap();
            receiveParam.put("ruleId",configTicketRuleVo.getId());
            Map usedParam = new HashMap();
            usedParam.put("ruleId",configTicketRuleVo.getId());
            usedParam.put("status",2);
            Map overdueParam = new HashMap();
            overdueParam.put("ruleId",configTicketRuleVo.getId());
            overdueParam.put("status",3);
            if(configTicketRuleVo.getTicketType() ==1){
                configTicketRuleVo.setReceiveCount(couponMapper.statCouponCount(receiveParam));
                configTicketRuleVo.setUsedCount(couponMapper.statCouponCount(usedParam));
                configTicketRuleVo.setOverdueCount(couponMapper.statCouponCount(overdueParam));
            }
            else if(configTicketRuleVo.getTicketType() ==2){
                configTicketRuleVo.setReceiveCount(ticketMapper.statTicketCount(receiveParam));
                configTicketRuleVo.setUsedCount(ticketMapper.statTicketCount(usedParam));
                configTicketRuleVo.setOverdueCount(ticketMapper.statTicketCount(overdueParam));
            }
        }
        return new PageList(list);
    }

    @Override
    public int insertRule(SysConfigTicketRule rule) {
        ticketRuleMapper.insertRule(rule);
        return rule.getId();
    }

    @Override
    public void updateStatus(Integer ruleId, Integer newStatus) {
        SysConfigTicketRule rule = ticketRuleMapper.selectByPrimaryKey(ruleId);
        if (rule != null) {
            Integer oldStatus = rule.getStatus();
            //当点击激活时判断开始时间是否可激活
            if (newStatus == 2 && rule.getBeginTime().compareTo(new Date()) > 0) {
                newStatus = 1;
            }
            ticketRuleMapper.updateStatus(ruleId, oldStatus, newStatus);
            //立即发送
            if(oldStatus ==1 && newStatus ==2) {
                if (rule.getNid().equals(Constants.send_intime_nid)) {
                    if (rule.getUsersType() == 0) {
                        Map map = new HashMap();
                        List<UserVo> list = basUserMapper.getList(map);
                        ticketModule.setIntime(rule, list);
                    } else if (rule.getUsersType() == 1) {
                        Map map = new HashMap();
                        map.put("ruleId",rule.getId());
                        List<SysConfigTicketUser> list = ticketUserMapper.getList(map);
                        ticketModule.setIntime(rule, list);
                    }
                }
            }
        }


    }

    @Override
    public void updateRule(SysConfigTicketRule rule) {
        ticketRuleMapper.updateRule(rule);
    }

    @Override
    public void updateRuleTimerStatus(Map map) {
        ticketRuleMapper.updateRuleBeginTimerStatus(map);
        ticketRuleMapper.updateRuleEndTimerStatus(map);
    }

}
